import time, thread
from subprocess import call, Popen, PIPE

class UnitTest():
	def __init__(self, num, cat, subcat, desc, behave, initial):
		self.number = num
		self.description = desc
		self.behavior = behave
		self.category = cat
		self.subcategory = subcat
		self.initial = initial
		self.passed = 0
		self.failed = 0
		self.script = 0
		self.reason = ""
		self.expectedOutput = []
		self.fileName = ""
		self.created = 0
		self.tcpProfile = ""

	def defineTest(self, URL):
		try:
			thread.start_new_thread(self.startProxy, (self.number,))
			thread.start_new_thread(self.startBrowser, (URL,))
			
			expectedText = self.getExpectedOutput()
			for elem in expectedText:
				self.expectedOutput.append(elem)
			self.created = 1
		except:
			print "failed to define test"

	def startProxy(self, number):
		cmd = ['java net.grinder.TCPProxy -console -http > %s.py' % number]	
		Popen(cmd, stdout=PIPE)

	def startBrowser(self, URL):
		time.sleep(5)
		cmd = ['firefox -profile %s' % self.tcpProfile + " " + URL]
		Popen(cmd, stdout=PIPE)

	def getExpectedOutput(self):
		# start input dialog
		# collect and return input when submitted
		return ['0']

	def testPass(self):
		self.passed += 1
		
	def testFail(self, reason):
		self.failed += 1
		self.reason = reason

	def reset(self):
		self.passed, self.failed = 0, 0
